package com.zzptc.sky.translate;

import android.app.ProgressDialog;
import android.graphics.drawable.RippleDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zzptc.sky.translate.ben.Register;

public class RegistrationActivity extends AppCompatActivity {
Toolbar toolbar_registration;
    private ProgressDialog mDialog;
    private EditText name,password,email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        toolbar_registration= (Toolbar) findViewById(R.id.toolbar_registration);
        password = (EditText) findViewById(R.id.registration_password);
        name = (EditText) findViewById(R.id.registration_username);
        email = (EditText) findViewById(R.id.registration_email);
        setSupportActionBar(toolbar_registration);
        //显示为已启用的setDisplayHomeAsUpEnabled
        //获得支持动作条 SupportActionBa
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDialog = new ProgressDialog(this);
        mDialog.setMessage("正在注册...");
        toolbar_registration.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.button_yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtil.get("http://apicloud.mob.com/user/rigister?key=1f45339301beb&username="+name.getText().toString().trim()+"&password="+password.getText().toString().trim()+"&email="+email.getText().toString().trim(),handler);
                mDialog.show();
            }
        });
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Gson gson = new Gson();
            Register register = gson.fromJson(msg.obj.toString(),Register.class);
            mDialog.cancel();
            if(register.getRetCode().trim().equals("200")){
                Toast.makeText(RegistrationActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            }
            switch (Integer.parseInt(register.getRetCode().trim())){
                case 22802: Toast.makeText(RegistrationActivity.this, "用户名不允许为空", Toast.LENGTH_SHORT).show(); break;
                case 22803: Toast.makeText(RegistrationActivity.this, "密码不允许为空", Toast.LENGTH_SHORT).show(); break;
                case 22806: Toast.makeText(RegistrationActivity.this, "此用户名已存在", Toast.LENGTH_SHORT).show(); break;
                case 22807: Toast.makeText(RegistrationActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); break;
                case 22810: Toast.makeText(RegistrationActivity.this, "用户未登录或token已过期", Toast.LENGTH_SHORT).show(); break;
                case 22817: Toast.makeText(RegistrationActivity.this, "email不允许为空", Toast.LENGTH_SHORT).show(); break;
                case 22818: Toast.makeText(RegistrationActivity.this, "email不符合邮箱格式", Toast.LENGTH_SHORT).show(); break;
                case 22823: Toast.makeText(RegistrationActivity.this, "此邮箱已存在", Toast.LENGTH_SHORT).show(); break;
            }
        }
    };
}
